//require cn1-native-controls
package com.codename1.samples;

import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.nui.NSelect;
import com.codename1.nui.NTextField;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.Font;
import com.codename1.ui.TextField;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BorderLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class NativeControlsSample {

    
    private Form current;
    private Resources theme;
    
    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);
        //NSelect.setUseLightWeightWidget(true);
        theme = UIManager.initFirstTheme("/theme");
        
        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);
        
        // Pro only feature
        Log.bindCrashProtection(true);
        
        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }
    
    
    public void webviewLoginTest() {
        Form f= new Form("Test Webview Login", new BorderLayout());
        String html = "<!doctype html>"
        + "<html><body><table><tr><td>Username:</td><td><input type='text' autocomplete='username'/></td></tr><tr><td>Password</td>"
        + "<td><input type='password' autocomplete='current-password'/></td></tr></body></html>";
        BrowserComponent c = new BrowserComponent();
        c.setPage(html, "https://weblite.ca/");
        f.add(BorderLayout.CENTER, c);
        f.show();
        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        if (false) {
            webviewLoginTest();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        
        hi.add(new Label("Hi World"));
        Label result = new Label();
        NSelect<String> select = new NSelect<>();
        $(select).selectAllStyles()
        .setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_LIGHT, 4f))
        .setFgColor(0xff0000)
        .setBgColor(0x00ff00)
        ;
        select.setOptions("Red", "Green", "Blue", "Orange");
        select.addSelectionListener((i1, i2)->{
            result.setText(select.getOption(select.getSelectedIndex()));
            hi.revalidateWithAnimationSafety();
        });
        hi.add(result);
        
        
        
        hi.add(select);
        hi.add("Text fields");
        hi.add("Username:");
        NTextField tf1 = new NTextField(TextField.USERNAME);
        System.out.println("Setting font to main light 15mm");
        tf1.getAllStyles().setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_LIGHT, 15f));
        System.out.println("Finished setting font");
        tf1.getAllStyles().setFgColor(0x003300);
        tf1.getAllStyles().setBgTransparency(255);
        tf1.getAllStyles().setBgColor(0xcccccc);
        tf1.getAllStyles().setAlignment(CENTER);
        hi.add(tf1);
        hi.add("Password:");
        NTextField tf2 = new NTextField(TextField.PASSWORD);
        hi.add(tf2);
        hi.add("Email:");
        NTextField emailField = new NTextField(TextField.EMAILADDR);
        hi.add(emailField);
        
        tf1.addActionListener(e->{
            //tf2.setText(tf1.getText());
        });
        tf1.addChangeListener(e->{
            result.setText(tf1.getText());
            hi.revalidateWithAnimationSafety();
        });
        tf2.addActionListener(e->{
            Log.p("Action listener fired on password field");
            result.setText(tf2.getText());
            hi.revalidateWithAnimationSafety();
        });
        tf2.addDoneListener(e->{
            Log.p("Done was clicked!!!");
        });
        
        hi.add(new TextField("LW field 1"));
        hi.add(new TextField("LW field 2"));
        hi.add(new TextField("LW field 3"));
        
        Button submit = new Button("Login");
        submit.addActionListener(e->{
            Form f = new Form("Logged In", BoxLayout.y());
            Toolbar tb =new Toolbar();
            f.setToolbar(tb);
            tb.setBackCommand(new Command("") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    hi.showBack();
                }
            });
            f.add("Logged In!!!");
            f.show();
        });
        hi.add(submit);
        hi.show();
    }
    
    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
